const db = require('../db/index');

// 从数据库取到上课时间与现在时间判断是否迟到
exports.testDate = (req, res) => {
    // 查询数据库的上课时间
    // res.send('成功')
    var classDate = null
    const userinfo = req.body
    const sql2 = 'select * from class'
    db.query(sql2, (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.cc('查询课表失败')
        classDate = results
        const sql = ' select * from class where class_id=?'
        db.query(sql, userinfo.id, (err, results) => {
            if (err) return res.cc(err)
            if (results.length !== 1) return res.cc('不存在该课')
                // console.log(results[0].class);
            res.send({
                status: 200,
                msg: '成功',
                data: results[0].class,
                class: classDate
            })
        })
    })


}

// 没迟到的情况下把出勤改成true
exports.checkDate = (req, res) => {
    const userinfo = req.body
    const sql = 'UPDATE studentmsg t SET t.attendendance = true WHERE t.id = ?'
    db.query(sql, userinfo.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('修改考勤失败')
        res.send({
            status: 200,
            msg: '成功',
        })
    })
}

exports.checkDatelate = (req, res) => {
    const userinfo = req.body
    const sql = 'UPDATE studentmsg t SET t.late = true WHERE t.id = ?'
    db.query(sql, userinfo.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('修改考勤失败')
        res.send({
            status: 200,
            msg: '成功',
        })
    })
}